import QtQuick 2.0
//import QtQuick 2.4
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2
import "../../painter.js" as Painter
import QtGraphicalEffects 1.0

Rectangle {
    width:parent.with
    height:parent.height
    color: "#151515"
    ListModel{
        id:tubeInDatasModel
    }
    ListModel{
        id:tubeOutDatasModel
    }


    //test datas area
    Rectangle{
        id:tableArea
        width: parent.width
        height: parent.height-100
        anchors.horizontalCenter: parent.horizontalCenter
        color:"#efefef"
        anchors.bottom: parent.bottom
        Canvas{
            id:tubeincanvas
            width: parent.width-70
            anchors.right: parent.right
            height: parent.height/2
            onPaint: {
                Painter.paintCoordinate(tubeincanvas);
            }
            Component.onCompleted: requestPaint()
            Text{
                text:"入管结焦诊断图表    管号:"+""
                anchors.horizontalCenter: parent.horizontalCenter
                y:10
                font.pixelSize: 25
                color: "#12aaef"
            }
        }

        Canvas{
            id:tubeoutcanvas
            width: parent.width-70
            anchors.right: parent.right
            height: parent.height/2
            anchors.bottom: parent.bottom
            onPaint: {
                Painter.paintCoordinate(tubeoutcanvas);
            }
            Component.onCompleted: requestPaint()
            Text{
                text:"出管结焦诊断图表    管号:"+""
                anchors.horizontalCenter: parent.horizontalCenter
                y:10
                font.pixelSize: 25
                color: "#12aaef"
            }
        }

        Rectangle{
            width: parent.width
            height: 1
            color: "#202020"
            anchors.verticalCenter: parent.verticalCenter

        }
    }

    //sortor area
    Rectangle{
        id:sortarea
        width: parent.width
        height: 100
        color: "#d0d0d0"

        //forum num edit field
        Text {
            id: forumlabel
            text: qsTr("炉号")
            x:250
            y:20
            font.pixelSize: 20
            color: "#1277ef"
        }
        TextField{
            id:forumedit
            width: 60
            height: 25
            anchors.verticalCenter: forumlabel.verticalCenter
            anchors.left: forumlabel.right
            anchors.leftMargin: 15
            font.pixelSize: 20
            validator: IntValidator {bottom: 1; top: 20;}
            text:"5"
            style:TextFieldStyle{
                textColor: "#12aabb"
                background: Rectangle {
                    radius: 3
                    implicitWidth: 60
                    implicitHeight: 24
                    border.color: "#333"
                    border.width: 1
                    color:"#666666"
                }

            }
        }

        //tube num edit field
        Text {
            id: tubelabel
            text: qsTr("管号")
            x:250
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 20
            font.pixelSize: 20
            color: "#1277ef"
        }
        TextField{
            id:tubeedit
            width: 60
            height: 25
            anchors.verticalCenter: tubelabel.verticalCenter
            anchors.left: tubelabel.right
            anchors.leftMargin: 15
            font.pixelSize: 20
            text:"1"
            validator: IntValidator {bottom: 1; top: 48;}
            style:TextFieldStyle{
                textColor: "#12aabb"
                background: Rectangle {
                    radius: 2
                    implicitWidth: 60
                    implicitHeight: 24
                    border.color: "#333"
                    border.width: 1
                    color:"#666666"
                }
            }
        }

        //from date
        Text{
            id:fromDateText
            text: qsTr("起始时间:")
            anchors.left: tubeedit.right
            anchors.leftMargin: 120
            y:20
            font.pixelSize: 20
            color: "#1277ef"
        }
        TextField{
            id:fyearedit
            width: 80
            height: 25
            anchors.verticalCenter: fromDateText.verticalCenter
            anchors.left: fromDateText.right
            anchors.leftMargin: 15
            font.pixelSize: 20
            validator: IntValidator {bottom: 2000; top: 9999;}
            style:TextFieldStyle{
                textColor: "#12aabb"
                background: Rectangle {
                    radius: 2
                    implicitWidth: 60
                    implicitHeight: 24
                    border.color: "#333"
                    border.width: 1
                    color:"#666666"
                }
            }


    }
        Text{
            id:fyearlabel
            text: "年"
            font.pixelSize: 20
            color: "#1277ef"
            y:20
            anchors.left: fyearedit.right
            anchors.leftMargin: 15
        }
        TextField{
            id:fmothedit
            width: 80
            height: 25
            anchors.verticalCenter: fromDateText.verticalCenter
            anchors.left: fyearlabel.right
            anchors.leftMargin: 15
            font.pixelSize: 20
            validator: IntValidator {bottom: 1; top: 12;}
            style:TextFieldStyle{
                textColor: "#12aabb"
                background: Rectangle {
                    radius: 2
                    implicitWidth: 60
                    implicitHeight: 24
                    border.color: "#333"
                    border.width: 1
                    color:"#666666"
                }
            }


    }
        Text{
            id:fmothlabel
            text: "月"
            font.pixelSize: 20
            color: "#1277ef"
            y:20
            anchors.left: fmothedit.right
            anchors.leftMargin: 15
        }
        TextField{
            id:fdayedit
            width: 80
            height: 25
            anchors.verticalCenter: fromDateText.verticalCenter
            anchors.left: fmothlabel.right
            anchors.leftMargin: 15
            font.pixelSize: 20
            validator: IntValidator {bottom: 1; top: 31;}
            style:TextFieldStyle{
                textColor: "#12aabb"
                background: Rectangle {
                    radius: 2
                    implicitWidth: 60
                    implicitHeight: 24
                    border.color: "#333"
                    border.width: 1
                    color:"#666666"
                }
            }


    }
        Text{
            id:fdaylabel
            text: "日"
            font.pixelSize: 20
            color: "#1277ef"
            y:20
            anchors.left: fdayedit.right
            anchors.leftMargin: 15
        }

        //to date
        Text{
            id:tromDateText
            text: qsTr("结束时间:")
            anchors.left: tubeedit.right
            anchors.leftMargin: 120
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 20
            font.pixelSize: 20
            color: "#1277ef"
        }
        TextField{
            id:tyearedit
            width: 80
            height: 25
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 20
            anchors.left: tromDateText.right
            anchors.leftMargin: 15
            font.pixelSize: 20
            validator: IntValidator {bottom: 2000; top: 9999;}
            style:TextFieldStyle{
                textColor: "#12aabb"
                background: Rectangle {
                    radius: 2
                    implicitWidth: 60
                    implicitHeight: 24
                    border.color: "#333"
                    border.width: 1
                    color:"#666666"
                }
            }


    }
        Text{
            id:tyearlabel
            text: "年"
            font.pixelSize: 20
            color: "#1277ef"
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 20
            anchors.left: fyearedit.right
            anchors.leftMargin: 15
        }
        TextField{
            id:tmothedit
            width: 80
            height: 25
            anchors.verticalCenter: tromDateText.verticalCenter
            anchors.left: tyearlabel.right
            anchors.leftMargin: 15
            font.pixelSize: 20
            validator: IntValidator {bottom: 1; top: 12;}
            style:TextFieldStyle{
                textColor: "#12aabb"
                background: Rectangle {
                    radius: 2
                    implicitWidth: 60
                    implicitHeight: 24
                    border.color: "#333"
                    border.width: 1
                    color:"#666666"
                }
            }


    }
        Text{
            id:tmothlabel
            text: "月"
            font.pixelSize: 20
            color: "#1277ef"
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 20
            anchors.left: tmothedit.right
            anchors.leftMargin: 15
        }
        TextField{
            id:tdayedit
            width: 80
            height: 25
            anchors.verticalCenter: tromDateText.verticalCenter
            anchors.left: tmothlabel.right
            anchors.leftMargin: 15
            font.pixelSize: 20
            validator: IntValidator {bottom: 1; top: 31;}
            style:TextFieldStyle{
                textColor: "#12aabb"
                background: Rectangle {
                    radius: 2
                    implicitWidth: 60
                    implicitHeight: 24
                    border.color: "#333"
                    border.width: 1
                    color:"#666666"
                }
            }


    }
        Text{
            id:tdaylabel
            text: "日"
            font.pixelSize: 20
            color: "#1277ef"
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 20
            anchors.left: tdayedit.right
            anchors.leftMargin: 15
        }

        //confirm button
        Rectangle{
            width: 120
            height: 35
            anchors.right: parent.right
            anchors.rightMargin: 60
            anchors.verticalCenter: parent.verticalCenter
            radius: 2
            color: confirmma.containsMouse?"#fafafa":"#f0f0f0"
            border.width: 1
            border.color: confirmma.containsMouse?"#666666":"#777777"
            Text{
                text: "确定"
                anchors.centerIn: parent
                font.pixelSize: 20
                color: "#666666"
                font.bold: true
            }

            MouseArea{
                id:confirmma
                anchors.fill: parent
                hoverEnabled: true
                onClicked: {
                    tubeInDatasModel.clear();
                    tubeOutDatasModel.clear();
                    if(
                            fyearedit.text!=null&&
                            fmothedit.text!=null&&
                            fdayedit.text!=null&&
                            tyearedit.text!=null&&
                            tmothedit.text!=null&&
                            tdayedit.text!=null&&
                            forumedit.text!=null&&
                            tubeedit.text!=null){
                        var fromDate=new Date();
                        var toDate=new Date();
                        fromDate.setFullYear(fyearedit.text,Number(fmothedit.text)-1,fdayedit.text);
                        toDate.setFullYear(tyearedit.text,Number(tmothedit.text)-1,tdayedit.text);
                        server.testDatas(Number(forumedit.text),Number(tubeedit.text),fromDate,toDate);

                        //get datas
                        for(var i=0;i<server.get_test_in_datas_length();i++){
                            tubeInDatasModel.append({
                                                        "temp":server.get_test_in_temp(i),
                                                        "time":server.get_test_in_time(i)
                                                    });
                        }
                        for(var i=0;i<server.get_test_out_datas_length();i++){
                            tubeOutDatasModel.append({
                                                        "temp":server.get_test_out_temp(i),
                                                        "time":server.get_test_out_time(i)
                                                    });
                        }
                        Painter.drawTestLine(tubeincanvas,tubeInDatasModel);
                        Painter.drawTestLine(tubeoutcanvas,tubeOutDatasModel);
                        console.log("tube out count",tubeOutDatasModel.count);
                        console.log("tube in count",tubeInDatasModel.count);
                        console.log(fromDate);
                        console.log(toDate);

                    }
                }
            }
        }
    }

    //Shader
    Glow{
        anchors.fill: sortarea
        source: sortarea
        color: "#202020"
        samples: 15
        radius: 15
        spread: 0.2
        fast: true
        transparentBorder: true
        cached: true
    }

}
